#include <cstdio>
#include <map>
#include <cstring>
#include <iostream>
using namespace std;
struct node
{
	map<string,int>mp2;
};
int main()
{
	int t;
	scanf("%d",&t);
	map<string,node>mp1;
	map<string,node>::iterator it1;
	map<string,int>::iterator it2;
	string fruit,place;
	int count;
	while(t--)
	{
		int n;
		scanf("%d",&n);
		mp1.clear();
		while(n--)
		{
			cin>>fruit>>place>>count;
			mp1[place].mp2[fruit] += count;
		}
		for(it1 = mp1.begin();it1!=mp1.end();it1++)
		{
			cout<< it1->first<<endl;
			for(it2 = it1->second.mp2.begin();it2!=it1->second.mp2.end();it2++)
			{
				cout<<"   |----"<<it2->first<<"("<<it2->second<<")"<<endl;
			}
		}
		if(t != 0)cout<<endl;
	}
} 
